/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package RRHH_controlador;

import Conexion.Conexion;
import RRHH_Modelo.RHH_REGIONAL;
import RRHH_Modelo.RHH_USUARIO;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author equipo_4
 */
@WebServlet(name = "RRHH_REGIONAL_CONTROLLER", urlPatterns = {"/RRHH_REGIONAL_CONTROLLER"})
public class RRHH_REGIONAL_CONTROLLER extends HttpServlet {
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String proceso=request.getParameter("proceso");
            RHH_USUARIO usuario = ((RHH_USUARIO)request.getSession().getAttribute("usr"));
            Conexion con = new Conexion(usuario.getUSUARIO(), usuario.getPASSWORD());
            String html="";
            switch(proceso){
                case "GuardarNuevo":
                    html=GuardarNuevo(request,con);
                    break;
                case "eliminacionRegional":
                    html=eliminacionSubProceso(con,request);
                    break;
                case "cambiarRegional":
                    html=cambiarRegional(con,request);
                    break;
            }
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write(html);
            con.Close();
        } catch (SQLException ex) {
            Logger.getLogger(RRHH_REGIONAL_CONTROLLER.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public String eliminacionSubProceso(Conexion con, HttpServletRequest request) throws SQLException{
        int id = Integer.parseInt(request.getParameter("EliminarRegional"));
        RHH_REGIONAL depto = new RHH_REGIONAL(con);
        depto.setCon(con);
        depto = depto.BuscarXid(id);
        depto.EliminarXid();
        return actualizarProcesos(con);
    }
    public String GuardarNuevo(HttpServletRequest request, Conexion con) throws SQLException{
        String desc = request.getParameter("descripcion");
        String acronimo = request.getParameter("acronimo");
        RHH_REGIONAL regional = new RHH_REGIONAL(0, desc, acronimo);
        regional.setCon(con);
        regional.Insertar();
        return actualizarProcesos(con);
    }
    public String actualizarProcesos(Conexion con) throws SQLException{
        RHH_REGIONAL regional = new RHH_REGIONAL(con);
        regional.setCon(con);
        List<RHH_REGIONAL> Lista = regional .Todos();
        String html="";
        for(int i =0; i<Lista.size(); i++)
        {
           html+="<tr>";
           html+="<td>";
           html+=Lista.get(i).getDESCRIPCION();
           html+="</td>";
           html+="<td>";
           html+=Lista.get(i).getACRONIMO();
           html+="</td>";
           html+="<td>";
           html+="<a href='#' onclick=\"eliminarRegional("+Lista.get(i).getID()+")\"><img src='../img/iconos/Actualizar2.png' /></a>";
           html+="</tr>";
        }
        return html;
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String cambiarRegional(Conexion con, HttpServletRequest request) throws SQLException {
        RHH_REGIONAL reg = new RHH_REGIONAL(con);
        reg.setCon(con);
        double id = Double.parseDouble(request.getParameter("idreg"));
        double idold = Double.parseDouble(request.getParameter("idvieja"));
        String html="";
        if (id!=idold) 
        {            
            reg.ActivarRegionalContable(id);
            reg.DesactivarRegionalContable(idold);
            List<RHH_REGIONAL> Lista = reg.Todos();
            double regcon = 0;

            for(int i =0; i<Lista.size(); i++)
            {                                                                                               
                html+="<tr>";
                html+="<td>";
                html+=Lista.get(i).getDESCRIPCION();
                html+="</td>";
                html+="<td>";
                html+=Lista.get(i).getACRONIMO();
                html+="</td>";
                html+="<td>";
                html+="<a href='#' onclick=\"eliminarRegional("+Lista.get(i).getID()+")\"><img src='../img/iconos/Actualizar2.png' /></a>"; 
                html+="</td>"; 
                html+="<td>"; 
                if(Lista.get(i).getREGIONAL_CONTABLE()==0) 
                { 
                    regcon = Lista.get(i).getID(); 
                    html+="<a href='#' onclick=\"eliminarRegional("+Lista.get(i).getID()+")\">SI</a>"; 
                } 
                else 
                    html+="<a href='#' onclick=\"CambiarRegional("+Lista.get(i).getID()+","+regcon+")\">NO</a>"; 
                html+="</td>"; 
                html+="</tr>"; 
            } 
            html+="<tr><td id='regcon' style='display:none'>"+regcon+"</td></tr>"; 
            con.commit(); 
        }
         
        return html;
    }
}
